import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  Input,
  Upload,
  Space,
  Select,
  message
} from 'antd'
import { PlusOutlined } from '@ant-design/icons'
import ReactQuill from 'react-quill'
import 'react-quill/dist/quill.snow.css'
import { Link, useNavigate, useSearchParams } from 'react-router-dom'
import './index.scss'
import { useEffect, useState } from 'react'
import {postArticleAPI } from '@/api/publish'
import { useChannelList } from '@/hooks/useChannelList'
import { getArticleDetailAPI, putArticleAPI } from '@/api/article'

const { Option } = Select

const Publish = () => {
  const navigate=useNavigate()
  const [params]=useSearchParams()
  const id=params.get('id')
  const [form]=Form.useForm()
useEffect(()=>{
    const getFormData=async()=>{
      const res=await  getArticleDetailAPI(id)
      form.setFieldsValue({...res.data,type:res.data.cover.type})
      setType(res.data.cover.type)
      setIamge(res.data.cover.images.map(url=>{
        return {url}
      } ))
    }
    if(id){
      getFormData()
    }
 
  },[id,form])
  const {channelList}=useChannelList()
  const onFinish=async(e)=>{
    if(imageList.length!==type) return message.warning('图片类型和数量不一致')
    const articleData={
      ...e,
    cover:{
      type:type,
      images:imageList.map(item=>{
        if(item.response){
          return item.response.data.url
        }else {
          return item.url
        }
      } )
    }
  }
  if(id){
    await putArticleAPI(articleData)
  }else {
    await postArticleAPI(articleData)
  }
  message.success(id?'更新成功' :'发布成功')
  navigate('/article')
  }
  const file=(e)=>{
    console.log(e);
    setIamge(e.fileList)
    
  }
  const [imageList,setIamge]=useState([])
  const [type,setType]=useState(0)
  return (
    <div className="publish">
      <Card
        title={
          <Breadcrumb items={[
            { title: <Link to={'/'}>首页</Link> },
            { title:`${id? '编辑文章':'发布文章'}` },
          ]}
          />
        }
      >
        <Form
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 16 }}
          initialValues={{ type: 0 }}
          validateTrigger={['onBlur']}
          onFinish={onFinish}
          form={form}
        >
          <Form.Item
            label="标题"
            name="title"
            rules={[{ required: true, message: '请输入文章标题' }]}
          >
            <Input placeholder="请输入文章标题" style={{ width: 400 }} />
          </Form.Item>
          <Form.Item
            label="频道"
            name="channel_id"
            rules={[{ required: true, message: '请选择文章频道' }]}
          >
            <Select placeholder="请选择文章频道" style={{ width: 400 }}>
              {channelList.map(item=><Option key={item.id} value={item.id}>{item.name}</Option>)}
            </Select>
          </Form.Item>
          <Form.Item label="封面">
          <Form.Item name="type">
            <Radio.Group onChange={(e)=>setType(e.target.value)}>
              <Radio value={1}>单图</Radio>
              <Radio value={3}>三图</Radio>
              <Radio value={0}>无图</Radio>
            </Radio.Group>
          </Form.Item>
          {type!==0 &&    <Upload
            listType="picture-card"
            showUploadList
            action={'http://geek.itheima.net/v1_0/upload'}
            name='image'
            maxCount={type}
            onChange={file}
          >
            <div style={{ marginTop: 8 }}>
              <PlusOutlined />
            </div>
          </Upload>}
       
          </Form.Item>
                  <Form.Item
            label="内容"
            name="content"
            rules={[{ required: true, message: '请输入文章内容' }]}
          >
           <ReactQuill         
            className="publish-quill"
            theme="snow"
            placeholder="请输入文章内容"/> 
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 4 }}>
            <Space>
              <Button size="large" type="primary" htmlType="submit" >  
               {id?'更新文章': '发布文章'}
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default Publish